import { geoCoordMap } from '../../component/analysisPanel/template';

interface CityAvg {
    [city: string]: {
        count: number,
        avg: number
    }
}

export function collectSuccessRate(status: object): number {
    let successCounter = 0, failCounter = 0;
    for (let city in status) {
        for (let statusNum in status[city]) {
            if (statusNum === '200') {
                successCounter += status[city][statusNum];
            } else {
                failCounter += status[city][statusNum];
            }
        }
    }
    const counterSum = successCounter + failCounter;
    if (counterSum === 0) {
        return null;
    }
    return successCounter / counterSum;
}

export function collectAreaSuccessRate(status: object): number {
    let successCounter = 0, failCounter = 0;
    for (let statusNum in status) {
        if (statusNum === '200') {
            successCounter += status[statusNum];
        } else {
            failCounter += status[statusNum];
        }
    }
    const counterSum = successCounter + failCounter;
    if (counterSum === 0) {
        return null;
    }
    return successCounter / counterSum;
}

export function collectAllAvg(speedInfo: object): [number, CityAvg, number] {
    const cityAvg: CityAvg = {};
    for (let city in speedInfo) {
        let cityAllCount = 0, cityAllAvg = 0;
        for (let isp in speedInfo[city]) {
            cityAllCount += speedInfo[city][isp].count;
        }
        if (cityAllCount === 0) {
            continue;
        }
        for (let isp in speedInfo[city]) {
            cityAllAvg += speedInfo[city][isp].count / cityAllCount * speedInfo[city][isp].avg;
        }
        cityAvg[city] = {
            count: cityAllCount,
            avg: cityAllAvg
        }
    }
    let allAvg = 0, allCount = 0
    for (let city in cityAvg) {
        allCount += cityAvg[city].count;
    }
    for (let city in cityAvg) {
        allAvg += cityAvg[city].count / allCount * cityAvg[city].avg;
    }
    return [allAvg, cityAvg, allCount];
}


interface GeoChartData {
    data: object,
    maxAvg: number,
    maxCount: number
}


export function calcGeoChartData(cityAvg: CityAvg): GeoChartData {
    const geoChartData = [];
    let maxValue = 0;
    let maxCount = 0
    for (const city in cityAvg) {
        if (!geoCoordMap[city]) {
            continue;
        }
        geoChartData.push({
            name: city,
            value: [...geoCoordMap[city], cityAvg[city].avg.toFixed(2), cityAvg[city].count]
        });
        cityAvg[city].avg > maxValue ? maxValue = cityAvg[city].avg : null;
        cityAvg[city].count > maxCount ? maxCount = cityAvg[city].count : null;
    }
    return {
        data: geoChartData,
        maxAvg: maxValue,
        maxCount
    }
}

export function HorizonBarChartTemplate(title: string, seriesData: string[], axisData: string[]) {
    if (!seriesData || seriesData.length === 0) {
        return null;
    }
    return {
        title: {
            text: title,
            left: "center",
            textStyle:
                { fontSize: 14, color: "#808492", fontWeight: 500 }
        },
        color: ['#3b9ffc'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '30',
            right: '30',
            bottom: '20',
            containLabel: true
        },
        toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
        },
        calculable: true,
        yAxis: {
            splitLine: {
                show: false
            }, //去除网格线
            splitArea: {
                show: false
            }, //保留网格区域
            axisLine: {
                show: true,
                lineStyle: { //轴上的线样式
                    color: '#808492',
                    width: 0.6, //这里是为了突出显示加上的
                },
            },
            axisTick: {
                show: true
            },
            axisLabel: { //轴上的数据样式
                color: '#808492',
            },
            data: axisData,
        },
        xAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#ddd'
                }
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: "#999"
                },
            },
            nameTextStyle: {
                color: "#999"
            },
            splitArea: {
                show: false
            }
        },
        series: [{
            type: 'bar',
            barWidth: 20, // 柱形的宽度
            barGap: 0,
            data: seriesData,
            name: '延迟(ms)',
            itemStyle: {
                normal: {
                    barBorderRadius: [0, 2, 2, 0],
                }
            },
        }
        ]
    };
}

// 大小映射函数: 0~maxCount => 4~45
const MAXPX = 45;
const MINPX = 4;
function pxTrans(value: number, maxCount: number) {
    const px: number = (value / maxCount) * MAXPX;
    return px < MINPX ? MINPX : px;
}

export function speedGeoChartTemplate(title: string, data: GeoChartData, color = ['#C1E1FF', '#1890FF']) {
    if (!data) {
        return null;
    }

    return {
        title: {
            text: title,
            left: 'center',
            textStyle: {
                fontSize: 14,
                color: '#808492',
                fontWeight: 500,
            }
        },
        tooltip: {
            trigger: 'item',
            formatter: function (params) {
                return params.name + ' : ' + params.value[2] + 'ms\n' + '请求次数：' + params.value[3];
            }
        },
        legend: {
            orient: 'vertical',
            y: 'bottom',
            x: 'right',
            data: ['value'],
            textStyle: {
                color: '#101010'
            }
        },
        visualMap: {
            min: 0,
            max: data.maxAvg,
            calculable: true,
            dimension: 2,
            inRange: {
                color: color
            },
            textStyle: {
                color: '#101010'
            }
        },
        geo: {
            map: 'china',
            label: {
                emphasis: {
                    show: false
                }
            },
            roam: true,
            scaleLimit: {
                min: 1,
                max: 10
            },
            itemStyle: {
                normal: {
                    areaColor: '#e2e2e2',
                    borderColor: '#e1e1e1'
                },
                emphasis: {
                    areaColor: '#eee'
                }
            }
        },
        series: [
            {
                name: '访问量',
                type: 'scatter',
                coordinateSystem: 'geo',
                data: data.data,
                symbolSize: function (val) {
                    return pxTrans(val[3], data.maxCount);
                },
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: false
                    }
                },
                itemStyle: {
                    normal: {
                        color: color[1]
                    }
                }
            }
        ]
    };
}

export function calcCityDistributionSeriseData(thisCityDistribution, initSerise = [0, 0, 0, 0, 0]) {
    let seriseData = initSerise;
    for (let key in thisCityDistribution) {
        switch (key) {
            case 'S':
                seriseData[0] += thisCityDistribution[key]
                break;
            case 'A':
                seriseData[1] += thisCityDistribution[key]
                break;
            case 'B':
                seriseData[2] += thisCityDistribution[key]
                break;
            case 'C':
                seriseData[3] += thisCityDistribution[key]
                break;
            case 'D':
                seriseData[4] += thisCityDistribution[key]
                break;
        }
    }
    return seriseData;
}

export function calcAllDistribution(distribution) {
    let seriseData = [0, 0, 0, 0, 0];
    for (let city in distribution) {
        seriseData = calcCityDistributionSeriseData(distribution[city], seriseData);
    }
    return seriseData;
}